const gulp = require('gulp')
const px2rpx = require('gulp-px2rpx')
const sass = require('gulp-sass')(require('sass'))
const rename = require('gulp-rename')

function base(paths) {
  return gulp.src(paths).pipe(sass())
      .pipe(px2rpx({
          screenWidth: 750,  // 设计稿屏幕，默认750
          wxappScreenWidth: 750,  // 微信小程序屏幕，默认 750
          remPrecision: 6,  // 小数进准度，默认是6
        })
      )
      .pipe(rename(function(file) {
          file.extname = '.wxss'
        })
      )
      .pipe(gulp.dest(function(file) {
           return file.base  // 对应文件输出到其对应的目录
        })
       )
}

function components() {
  return base('components/**/*.scss')
}
function subpackages() {
  return base('subpackages/**/*.scss')
}
function app() {
  return base('app.scss')
}
function pages() {
  return base('pages/**/*.scss')
}
exports.default = function() {
  gulp.watch('styles/**/*.scss', gulp.series(components, subpackages, app, pages))
  gulp.watch('pages/**/*.scss', gulp.series(pages))
  gulp.watch('app.scss', gulp.series(app))
  gulp.watch('subpackages/**/*.scss', gulp.series(subpackages))
  gulp.watch('components/**/*.scss', gulp.series(components))
}